package com.suypower.carManage.comm;

import java.text.DecimalFormat;

public class ConstUtil {
	
	public static String DBNAME="Jarp";
	public static String DBNAMEODS="Jarp";
	public static String splitChar="@";
	//数据库类型
	public static String DBTYPE="SQLServer";
	//费用类型
	
	public static String COSTOFREPAIR="1";  //维修费用
        public static String COSTOFMAINTAIN="2"; //车辆保养费用
	public static String COSTOFREFUEL="3";  //加油费用
	public static String COSTOFEE="4";  //规费及其他费用
        public static String COSTOFTYRE="5"; //轮胎更换记录
        
         /**
     * 判断是否是数字
     *
     * @param args
     * @return
     */
    public static boolean isNumber(String args) {
        if (args == null) {
            return false;
        } else {
            return args.matches("-*\\d+\\.?\\d*");
        }

    }

    /**
     * 将字符串转化为double类型
     *
     * @param str
     * @return
     */
    public static Double convertToDouble(String str) {
        double b = 0;
        if (ConstUtil.isNumber(str)) {
            b = Double.valueOf(str);
        }
        return b;
    }

    public static Double convertToInteger(String str) {
        double b = 0;
        if (ConstUtil.isNumber(str)) {
            b = Integer.valueOf(str);
        }
        return b;
    }

    public static String convertToBigDecimal(String str) {
        try {
            Double result = Double.valueOf(str);
            DecimalFormat decimalFormat = new DecimalFormat("###0");//格式化设置  
            return decimalFormat.format(result);
        } catch (Exception e) {
            e.printStackTrace();
            return "0";
        }
    }

    /**
     * 计算平均数
     *
     * @param args
     * @return
     */
    public static double avg(String... args) {
        double avg = 0;
        double sum = 0;
        int count = 0;
        for (String str : args) {
            if (ConstUtil.isNumber(str)) {
                double d = ConstUtil.convertToDouble(str);
                sum = Arith.add(sum, d);
                count++;
            }
        }
        if (count > 0) {
            avg = Arith.div(sum, count, 2);
        }
        return avg;
    }
}
